
package com.ifcar.app.db;

import com.ifcar.app.rss.GetColumnsInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DBUtils {

    public static Cursor getDataFromNewsListTable(Context context, SQLiteDatabase db) {
        return db.query(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, new String[] {
            NewsSQLiteOpenHelper.NEWS_AUTO_ID + " AS _id," + NewsSQLiteOpenHelper.NEWS_COL_ID + ","
                    + NewsSQLiteOpenHelper.NEWS_DOC_ID + "," + NewsSQLiteOpenHelper.NEWS_IMG_SRC
                    + "," + NewsSQLiteOpenHelper.NEWS_DIGEST + ","
                    + NewsSQLiteOpenHelper.NEWS_REPLY_COUNT + "," + NewsSQLiteOpenHelper.NEWS_URL
                    + "," + NewsSQLiteOpenHelper.NEWS_TITLE + "," + NewsSQLiteOpenHelper.NEWS_PTIME
                    + "," + NewsSQLiteOpenHelper.NEWS_IS_HASIMG
        }, null, null, null, null, null, null);
    }

    public static void deleteDataToNewsListTabel(SQLiteDatabase db, String colId) {

        db.delete(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, NewsSQLiteOpenHelper.NEWS_COL_ID + "='"
                + colId + "'", null);

    }

    public static ArrayList<String> getSpecialArticleIds(SQLiteDatabase db, String colId) {

        Cursor cursor = db.query(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, new String[] {
            NewsSQLiteOpenHelper.NEWS_DOC_ID
        }, NewsSQLiteOpenHelper.NEWS_COL_ID + "='" + colId + "'", null, null, null,
                NewsSQLiteOpenHelper.NEWS_SPECIAL_INDEX + " ASC ,"
                        + NewsSQLiteOpenHelper.NEWS_AUTO_ID + " ASC ,"
                        + NewsSQLiteOpenHelper.NEWS_SPECIAL_TNAME + " DESC ", null);
        ArrayList<String> array = new ArrayList<String>();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String docid = cursor
                    .getString(cursor.getColumnIndex(NewsSQLiteOpenHelper.NEWS_DOC_ID));
            array.add(docid);
        }
        cursor.close();
        return array;
    }

    public static boolean isSpecial(SQLiteDatabase db, String docid, String columnid) {
        boolean rst = false;

        Cursor c = db.query(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, new String[] {
            NewsSQLiteOpenHelper.NEWS_SPECIAL_ID
        }, NewsSQLiteOpenHelper.NEWS_DOC_ID + "='" + docid + "' and "
                + NewsSQLiteOpenHelper.NEWS_COL_ID + "='" + columnid + "'", null, null, null, null,
                null);

        if (c.moveToFirst()) {
            String specialid = c.getString(c.getColumnIndex(NewsSQLiteOpenHelper.NEWS_SPECIAL_ID));
            if (null != specialid && !"".equals(specialid)) {
                rst = true;
            }
        }
        c.close();
        return rst;
    }

    public static void insertDataToNewsListTable(SQLiteDatabase db, String colId, String docid,
            String imgsrc, String digest, String replyCount, String url, String title,
            String ptime, String subtitle, String isFirst, String isRead, String hasImg,
            String specialid, String special_index, String sepcial_tname,String extendjson) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NewsSQLiteOpenHelper.NEWS_COL_ID, colId);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_DOC_ID, docid);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_IMG_SRC, imgsrc);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_DIGEST, digest);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_REPLY_COUNT, replyCount);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_URL, url);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_TITLE, title);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_PTIME, ptime);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_SUBTITLE, subtitle);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_IS_FIRST, isFirst);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_IS_READ, isRead);
        initialValues.put(NewsSQLiteOpenHelper.NEWS_IS_HASIMG, hasImg);

        if (null == specialid || "".equalsIgnoreCase(specialid)) {
            specialid = "";
        }
        initialValues.put(NewsSQLiteOpenHelper.NEWS_SPECIAL_ID, specialid);

        if (null == special_index || "".equalsIgnoreCase(special_index)) {
            special_index = "";
        }
        initialValues.put(NewsSQLiteOpenHelper.NEWS_SPECIAL_INDEX, special_index);

        if (null == sepcial_tname || "".equalsIgnoreCase(sepcial_tname)) {
            sepcial_tname = "";
        }
        initialValues.put(NewsSQLiteOpenHelper.NEWS_SPECIAL_TNAME, sepcial_tname);
        
        initialValues.put(NewsSQLiteOpenHelper.NEWS_EXTENDS_JSON, extendjson);
        
        db.insert(NewsSQLiteOpenHelper.NEWS_LIST_TABLE, null, initialValues);
    }

    public static void insertDataToCommentsListTable(SQLiteDatabase db, String colId, String docid,
            String replyCount, String title, String tie, String author, String replyBoard,
            String postid, String p, String ptime, String isRead) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_COL_ID, colId);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_DOC_ID, docid);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_REPLY_COUNT, replyCount);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_TITLE, title);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_TIE, tie);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_AUTHOR, author);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_REPLY_BOARD, replyBoard);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_POSTID, postid);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_FLOOR, p);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_IS_READ, isRead);
        initialValues.put(NewsSQLiteOpenHelper.COMMENTS_PTIME, ptime);
        db.insert(NewsSQLiteOpenHelper.COMMENTS_LIST_TABLE, null, initialValues);
    }

    public static void insertDataToPhotoListTable(SQLiteDatabase db, String photoColumn,String setid, String setname,
            String clientcover, String imgsum, String date,boolean replace) {
        if(replace){
            String sql = "select * from " + NewsSQLiteOpenHelper.PHOTO_LIST_TABLE + " where "
            + NewsSQLiteOpenHelper.PHOTO_SETID + "=? ";
            Cursor cursor = db.rawQuery(sql, new String[] {
                setid
            });
            int count = 0;
            if (null != cursor) {
        
                count = cursor.getCount();
            }
            cursor.close();
        
            if (count == 0) {
                ContentValues initialValues = new ContentValues();
                initialValues.put(NewsSQLiteOpenHelper.PHOTO_SETID, new Integer(setid));
                initialValues.put(NewsSQLiteOpenHelper.PHOTO_COLUMN, photoColumn);
                initialValues.put(NewsSQLiteOpenHelper.PHOTO_SETNAME, setname);
                initialValues.put(NewsSQLiteOpenHelper.PHOTO_CLIENTCOVER, clientcover);
                initialValues.put(NewsSQLiteOpenHelper.PHOTO_IMGSUM, imgsum);
                initialValues.put(NewsSQLiteOpenHelper.PHOTO_DATE, date);
        
                db.insert(NewsSQLiteOpenHelper.PHOTO_LIST_TABLE, null, initialValues);
            } else {
        
            }
        }else{
            ContentValues initialValues = new ContentValues();
            initialValues.put(NewsSQLiteOpenHelper.PHOTO_SETID, new Integer(setid));
            initialValues.put(NewsSQLiteOpenHelper.PHOTO_COLUMN, photoColumn);
            initialValues.put(NewsSQLiteOpenHelper.PHOTO_SETNAME, setname);
            initialValues.put(NewsSQLiteOpenHelper.PHOTO_CLIENTCOVER, clientcover);
            initialValues.put(NewsSQLiteOpenHelper.PHOTO_IMGSUM, imgsum);
            initialValues.put(NewsSQLiteOpenHelper.PHOTO_DATE, date);

            db.insert(NewsSQLiteOpenHelper.PHOTO_LIST_TABLE, null, initialValues);
        }
        
        
        

    }

    public static void insertDataToPicTable(SQLiteDatabase db, String channel,String setid, String json) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NewsSQLiteOpenHelper.PIC_SETID, setid);
        initialValues.put(NewsSQLiteOpenHelper.PIC_CHANNEL, channel);
        initialValues.put(NewsSQLiteOpenHelper.PIC_JSON, json);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sDateFormat.format(new java.util.Date());
        initialValues.put(NewsSQLiteOpenHelper.PIC_DATE, date);
        db.insert(NewsSQLiteOpenHelper.PIC_TABLE, null, initialValues);

    }

    public static void insertDataToReadStatusTable(SQLiteDatabase db, String docid, String colid,
            String status) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NewsSQLiteOpenHelper.ARTICLE_READ_DOC_ID, docid);
        initialValues.put(NewsSQLiteOpenHelper.ARTICLE_READ_COL_ID, colid);
        initialValues.put(NewsSQLiteOpenHelper.ARTICLE_READ_STATUS, status);
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sDateFormat.format(new java.util.Date());
        initialValues.put(NewsSQLiteOpenHelper.ARTICLE_READ_DATE, date);
        db.insert(NewsSQLiteOpenHelper.ARTICLE_READ_LIST_TABLE, null, initialValues);

    }

    public static void insertDataToCityTable(SQLiteDatabase db, String name, String province,
            String pinyin) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(NewsSQLiteOpenHelper.CITY_NAME, name);
        initialValues.put(NewsSQLiteOpenHelper.CITY_PROVINCE, province);
        initialValues.put(NewsSQLiteOpenHelper.CITY_PINYIN, pinyin);
        initialValues.put(NewsSQLiteOpenHelper.CITY_SELECT_COUNT, 0);
        db.insert(NewsSQLiteOpenHelper.CITY_TABLE, null, initialValues);

    }
    
    //插入视频列表数据到数据库
    public static void insertDataToVideoListTable(SQLiteDatabase db, String colId, String replyCount,
            String digest, String digestall,String title, String hits, String cover,
            String replyBoard, String replyid, String mp4_url, String length, String vurl,
            String vid, String ptime,String extendjson) {
        
        ContentValues initialValues = new ContentValues();
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_COL_ID, colId);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_REPLY_COUNT, replyCount);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_DIGEST, digest);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_DIGEST_ALL, digestall);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_TITLE, title);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_HITS, hits);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_COVER, cover);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_REPLY_BOARD, replyBoard);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_REPLY_ID, replyid);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_MP4_URL, mp4_url);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_LENGTH, length);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_VURL, vurl);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_VID, vid);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_PTIME, ptime);
        initialValues.put(NewsSQLiteOpenHelper.VIDEO_EXTENDS_JSON, extendjson);
        
        db.insert(NewsSQLiteOpenHelper.VIDEO_LIST_TABLE, null, initialValues);
    }
}
